package com.itheima.d1_stream;

import java.util.*;
import java.util.stream.Stream;

//目标：掌握Stream流对象的获取方式
public class Demo2 {
    public static void main(String[] args) {
        // 1、如何获取List集合的Stream流？
        List<String> list = new ArrayList<>();
        Collections.addAll(list, "张三丰", "张无忌", "周芷若", "赵敏", "张强");
        Stream<String> listStream = list.stream();
        //直接打印变量名，看到的是Stream流对象的地址值
        //System.out.println(listStream);
        //如果想遍历出Stream流中的数据，请使用forEach方法
        /*
        listStream.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
        */
        //可以结合Lambda或方法引用进行简化！
        list.forEach(System.out::println);
        System.out.println("-----------------");

        // 2、如何获取Set集合的Stream流？
        Set<String> set = new HashSet<>();
        Collections.addAll(set, "刘德华", "张曼玉", "蜘蛛精", "马德", "德玛西亚");
        Stream<String> setStream = set.stream();
        setStream.forEach(System.out::println);
        System.out.println("-----------------");

        // 3、如何获取数组的Stream流？
        String[] arr = {"张翠山", "东方不败", "唐大山", "独孤求败"};
        //快捷方式：数组名.stream
        Stream<String> arrStream = Arrays.stream(arr);
//        Stream<String> arr1 = Stream.of(arr);
        arrStream.forEach(System.out::println);
        System.out.println("-----------------");

        // 4、如何获取Map集合的Stream流？
        Map<String, Double> map = new HashMap<>();
        map.put("古力娜扎", 172.3);
        map.put("迪丽热巴", 168.3);
        map.put("马尔扎哈", 166.3);
        map.put("卡尔扎巴", 168.3);

        //4.1 获取key的Stream流
        Set<String> keys = map.keySet();
        Stream<String> keyStream = keys.stream();
        keyStream.forEach(System.out::println);
        System.out.println("-----------------");

        //4.2 获取value的Stream流
        Collection<Double> values = map.values();
        Stream<Double> valueStream = values.stream();
        valueStream.forEach(System.out::println);
        System.out.println("-----------------");

        //4.3 获取键值对的Stream流
        Set<Map.Entry<String, Double>> entries = map.entrySet();
        Stream<Map.Entry<String, Double>> entryStream = entries.stream();
        entryStream.forEach(System.out::println);
        System.out.println("-----------------");

        //5.Stream接口自己还有一个静态方法of也可以获取零散数据、引用类型数组的Stream流对象
        Stream<Integer> numStream = Stream.of(1, 2, 3, 4, 5);
        numStream.forEach(System.out::println);
        System.out.println("-----------------");
        Double[] numArr = {1.1, 2.2, 3.3};
        Stream<Double> numArrStream = Stream.of(numArr);
        numArrStream.forEach(System.out::println);


    }
}
